package com.sinoglobal.ningxia.activity.vip;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.base.BaseActivity;
import com.sinoglobal.ningxia.beans.AdverVo;
import com.sinoglobal.ningxia.beans.RootVo;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.frame.FlyApplication;
import com.sinoglobal.ningxia.http.Code;
import com.sinoglobal.ningxia.http.ConnectionUtil;
import com.sinoglobal.ningxia.utils.ItktLog;
import com.sinoglobal.ningxia.utils.MContants;
import com.sinoglobal.ningxia.utils.SharedPreferenceUtil;
import com.sinoglobal.ningxia.utils.TextUtil;

import net.tsz.afinal.FinalBitmap;

/**
 * @author lms
 * @title: SuggestBackActivity.java
 * @package com.sinoglobal.ningxia.activity.vip
 * @description: 意见反馈页面
 * @date 2014-6-23 下午4:44:31
 */
public class SuggestBack extends BaseActivity {
    private EditText suggest;// 意见输入框
    private Button submit;// 提交按钮
    private String suggestStr;// 意见内容
    private ImageView adverImg, closeAdver;// 广告位,广告位的关闭按钮
    private AdverVo adver;// 广告信息的bean

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vip_suggest_back);
        openChenJInShi();//开启沉浸式
        init();
        downLoadAdver();// 下载广告信息
        setOnClick();// 点击事件
    }

    // 添加点击事件
    private void setOnClick() {
        submit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                suggestStr = suggest.getText().toString().trim();
                // 判断输入的内容不能
                if (TextUtil.stringIsNull(suggestStr)) {
                    showShortToastMessage(getResources().getString(R.string.input_suggestion));
                    return;
                }
                if (suggestStr.length() > 100) {
                    showShortToastMessage(getResources().getString(R.string.limit_words_num));
                    return;
                }
                if (isLoginIntentLoginActivity()) {
                    sendSuggest(SharedPreferenceUtil.getString(FlyApplication.context, "username"));
                } else {
                    sendSuggest(getResources().getString(R.string.tourist));
                }
            }
        });
        // 广告位的点击事件
        adverImg.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (!isNetworkConnected(FlyApplication.context)) {
                    showShortToastMessage(getResources().getString(R.string.no_connections));
                    return;
                }
                // 打开默认浏览器显示网页信息
                try {
                    Uri uri = Uri.parse(adver.getAd_url());
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        // 关闭广告位点击事件
        closeAdver.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                adverImg.setVisibility(View.GONE);
                closeAdver.setVisibility(View.GONE);
            }
        });
    }

    protected void sendSuggest(final String username) {
        new ItktAsyncTaskWithDialog<Void, Void, RootVo>(getResources().getString(R.string.sending), true, true) {

            @Override
            public RootVo before(Void... params) throws Exception {
                /*
				 * String contact = contactInfo.getText().toString(); if
				 * (TextUtil.stringIsNull(contact)) contact = "null";
				 */
                return RemoteImpl.getInstance().sendSuggest(username, suggestStr, null);
            }

            @Override
            public void after(RootVo result) {
                if (null == result) { // 数据为空，显示reLoad
                    return;
                }

                if (Code.CODE_SUCCESS != result.getCode()) {
                    showShortToastMessage(result.getMessage());
                    return;
                }
                finish();
            }

            @Override
            public void exception() {

            }
        }.execute();
    }

    private void init() {
        titleView.setText(R.string.suggest_back);
        submit = (Button) findViewById(R.id.submit_btn);
        //设置广告位的高度
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        adverImg = (ImageView) findViewById(R.id.advertising);
        RelativeLayout.LayoutParams params = (LayoutParams) adverImg.getLayoutParams();
        params.height = dm.widthPixels * 43 / 320;
        adverImg.setLayoutParams(params);
        suggest = (EditText) findViewById(R.id.edit_suggest);
        closeAdver = (ImageView) findViewById(R.id.close_advertising);
    }

    private void downLoadAdver() {
        adver = new AdverVo();
        new ItktAsyncTaskWithDialog<Void, Void, AdverVo>(MContants.EMPTY_STRING, false, true) {

            @Override
            public AdverVo before(Void... params) throws Exception {
                return RemoteImpl.getInstance().downLoadAdver();
            }

            @Override
            public void after(AdverVo result) {
                if (null == result) { // 数据为空
                    return;
                }
                if (Code.CODE_SUCCESS != result.getCode()) {// 判断返回的是否需要更新的code值
                    return;
                }
                adver.setAd_img(result.getAd_img());
                adver.setAd_url(result.getAd_url());
                String url = ConnectionUtil.localUrl + adver.getAd_img();
                ItktLog.i("--------ad   url-------->>>>>>>>>>>>" + url);
                if (MContants.EMPTY_STRING.equals(adver.getAd_img())) {// 获取图片地址为空时，显示默认图片
                    adverImg.setVisibility(View.GONE);
                } else {
                    adverImg.setVisibility(View.VISIBLE);
                    FinalBitmap fb = FinalBitmap.create(FlyApplication.context);
                    fb.display(adverImg, url);// 将图片显示在广告位上
                    closeAdver.setVisibility(View.VISIBLE);// 设置关闭按钮可见
                }
                SharedPreferenceUtil.saveString(FlyApplication.context, "ad_name", result.getAd_name());
                SharedPreferenceUtil.saveString(FlyApplication.context, "ad_img", result.getAd_img());
                SharedPreferenceUtil.saveString(FlyApplication.context, "ad_url", result.getAd_url());
            }

            @Override
            public void exception() {
            }
        }.execute();
    }

    // 判断是否连接网络
    public boolean isNetworkConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }
}
